package com.happyfamily.springboot.crowdsourcingplatform.controller.weng;

import com.happyfamily.springboot.crowdsourcingplatform.base.result.PageTableRequest;
import com.happyfamily.springboot.crowdsourcingplatform.base.result.Results;
import com.happyfamily.springboot.crowdsourcingplatform.dto.NeedDto;
import com.happyfamily.springboot.crowdsourcingplatform.model.Need;
import com.happyfamily.springboot.crowdsourcingplatform.service.weng.BackNeedService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/api-back/need")
@Slf4j
public class NeedController
{
	@Autowired
	private BackNeedService backNeedService;

	@GetMapping("/list")
	@ResponseBody
	@ApiOperation(value = "后台分页获取需求", notes = "后台分页获取需求", httpMethod = "GET", response = Results.class)
	public Results<Need> getAllNeedsByPage(PageTableRequest request)
	{
		request.countOffset();
		return backNeedService.getAllNeedsByPage(request.getOffset(), request.getLimit());
	}

	@GetMapping("/delete")
	@ResponseBody
	@ApiOperation(value = "后台删除需求", notes = "后台删除需求", httpMethod = "GET", response = Results.class)
	@PreAuthorize("hasAuthority('back:need:delete')")
	public Results<Need> deleteNeed(NeedDto needDto)
	{
		backNeedService.deleteNeed(needDto.getNid());
		return Results.success();
	}

	@GetMapping(value = "/verify")
	@PreAuthorize("hasAuthority('back:need:query')")
	@ApiOperation(value = "后台审核需求", notes = "后台审核需求", httpMethod = "GET", response = String.class)
	public String verifyNeed(Model model, Need need)
	{
		model.addAttribute("need", backNeedService.getNeedById(need.getNid()));
		return "backPage/need-verify";
	}

	@PostMapping(value = "/passVerify")
	@ResponseBody
	@ApiOperation(value = "后台需求审核通过", notes = "后台需求审核通过", httpMethod = "GET", response = Results.class)
	public Results passVerify(Need need)
	{
		return backNeedService.passVerify(need);
	}

	@PostMapping(value = "/unPassVerify")
	@ResponseBody
	@ApiOperation(value = "后台审核需求不通过", notes = "后台审核需求不通过", httpMethod = "GET", response = Results.class)
	public Results unPassVerify(Need need)
	{
		return backNeedService.unPassVerify(need);
	}

	@GetMapping("/findNeedByFuzzyNeedTitle")
	@ResponseBody
	@PreAuthorize("hasAuthority('back:need:query')")
	public Results<Need> getNeedByFuzzyNeedTitle(PageTableRequest requests, String ntitle, String verifyState, String tenderState, String startTime, String endTime)
	{
		requests.countOffset();
		return backNeedService.getNeedByFuzzyNeedTitle(ntitle, verifyState, tenderState, startTime, endTime, requests.getOffset(), requests.getLimit());
	}
}
